//
//  SettingsView.m
//  xKey Interface
//
//  Created by Guillaume SOURDRILLE on 19/02/12.
//  Copyright (c) 2012 Edenyorke. All rights reserved.
//

#import "SettingsView.h"

@implementation SettingsView
@synthesize titleLabel;
@synthesize bannerChoice;
@synthesize ipTextInput,messageUtils,gamerTagInput;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    //To have a background
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    [self.view addSubview:tv];
    [self.view sendSubviewToBack:tv];
    
    
    messageUtils =  [[MessageUtils alloc] init];
    [ipTextInput setText:ipXkey];
    
    banner = [messageUtils getValue:@"banner"];
    if ([banner isEqualToString:@"ON"]){
        [bannerChoice setOn:TRUE];
    }else{
        [bannerChoice setOn:FALSE];
    }
    
    NSString *gamerTagString = [messageUtils getValue:@"gamertag"];
    if (gamerTagString == nil || [gamerTagString isEqualToString:@""]){
        [gamerTagInput setText:@""];
    }else{
        [gamerTagInput setText:gamerTagString];

    }
    
    backButton.title = NSLocalizedString(@"settings.popover.back.button", @"");
    saveButton.title = NSLocalizedString(@"settings.popover.save.button", @"");
    ipLabel.text = NSLocalizedString(@"settings.popover.ip.label", @"");
    gamerTag.text = NSLocalizedString(@"settings.popover.gamertag.label", @"");
    bannerLabel.text = NSLocalizedString(@"settings.popover.banner.label", @"");
    titleLabel.text = NSLocalizedString(@"settings.popover.title", @"");
    
}

- (void)viewDidUnload
{
    [self setIpTextInput:nil];
    [self setBannerChoice:nil];
    [self setTitleLabel:nil];
    [self setGamerTagInput:nil];
    gamerTag = nil;
    [super viewDidUnload];
    messageUtils = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)processRetour {
    [self dismissModalViewControllerAnimated:YES];
}


- (id)initWithParam:(NSString *)param
{
    
    
    if ([[UIScreen mainScreen] bounds].size.height == 568) {
        self = [super initWithNibName:@"SettingsView4retina" bundle:nil];
        //this is iphone 5 xib
    } else {
        self = [super initWithNibName:@"SettingsView" bundle:nil];
    }
    if (self) {
        ipXkey=param;
        
    }
    return self;
}


- (IBAction)processSave {

    [messageUtils setValue:@"ipxkey" value:[ipTextInput text]];
    [messageUtils setValue:@"gamertag" value:[gamerTagInput text]];
    if (bannerChoice.on){
        [messageUtils setValue:@"banner" value:@"ON"];
    }else{
        [messageUtils setValue:@"banner" value:@"OFF"];
    }
    [self dismissModalViewControllerAnimated:YES];
    
}

@end
